package com.ecity.ui 
{
	import com.ecity.events.MenuEvent;
	import com.greensock.TweenLite;
	import flash.display.Bitmap;
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	/**
	 * ...
	 * @author Yicheng.Zhang
	 */
	public class LinkMenu extends Menu {
		[Embed(source = "/assets/border3l.png")] private var BorderdBg:Class;
		public var menuIndex:int;
		private var textfield:TextField;
		public var isSelected:Boolean = false;
		private var selectedBg:Bitmap;
		private var background:Sprite;
		public function LinkMenu(text:String, index:int) {
			addListener();
			this.menuHeight = 27;
			this.menuIndex = index;
			addLinkedText(text);
			selectedBg = new BorderdBg();

			background = new Sprite();
			this.addChildAt(background,0);
			this.bgColors = [0x0b162e, 0x0c1b47];
			this.alphas = [100, 100];
			this.drawbg();
			background.addChildAt(selectedBg, 0);
			selectedBg.y = menuIndex * menuHeight;
			selectedBg.alpha = 0;
			//hlBorder.alpha = 0;
		}
		
		private function addListener():void {
			this.addEventListener(MouseEvent.MOUSE_OVER, onHoverHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, onHoverOut);
			this.addEventListener(MouseEvent.CLICK, onClickHandler);
		}
		
		private function addLinkedText(text:String):void {
			textfield = new TextField();
			textfield.selectable = false;
			textfield.text = text;
			textfield.x = 150;
			textfield.textColor = 0x1a5dc2;
			textfield.height = menuHeight;
			textfield.y = menuIndex * menuHeight+4;
			this.addChild(textfield);
		}
		
		private function onHoverHandler(e:MouseEvent):void {
			if (!isSelected) {
				this.bgColors = [0x0b162e, 0x1c4694];
				drawbg();
				textfield.textColor = 0xffffff;
			}
		}
		
		private function onHoverOut(e:MouseEvent):void {
			if (!isSelected) {
				this.bgColors = [0x0c1836, 0x0b1834];
				drawbg();
				textfield.textColor = 0x1a5dc2;
			}
		}
		
		private function onClickHandler(e:MouseEvent):void {
			if (!isSelected) {
				TweenLite.to(textfield, 0.2, { x:100 } );
				TweenLite.to(selectedBg, 0.2, { alpha:1 } );
				isSelected = true;
				textfield.textColor = 0xa9ccff;
				this.dispatchEvent(new MenuEvent(MenuEvent.LINK_CHANGED));
			}
		}
		
		protected  function drawbg():void {
			background.graphics.clear();
			background.graphics.beginGradientFill(GradientType.LINEAR, bgColors, alphas, ratios,matrix);
			background.graphics.drawRect(0, menuIndex * menuHeight, menuWidth, menuHeight);
			background.graphics.endFill();
		}
		
		/**
		 * 恢复未选择样式
		 */
		public function backOriginal():void {
			if(isSelected){
				TweenLite.to(textfield, 0.2, { x: 150 } );
				TweenLite.to(selectedBg, 0.2, { alpha:0} );
				isSelected = false;
				this.bgColors = [0x0c1836, 0x0b1834];
				drawbg();
				textfield.textColor = 0x1a5dc2;
			}
		}
	}

}